package com.ygqh.baby.service.impl;
import java.util.Arrays;
import java.util.List;

import  org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import  org.springframework.stereotype.Service;
import  org.springframework.util.Assert;

import  com.ygqh.baby.ao.DataStatus;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.mapper.YgProductRecommendMapper;
import com.ygqh.baby.model.YgProductRecommendModel;
import  com.ygqh.baby.po.YgProductRecommend;
import  com.ygqh.baby.po.YgProductRecommendExample;
import  com.ygqh.baby.service.YgProductRecommendService;

@Service
public class YgProductRecommendServiceImpl implements YgProductRecommendService{
	
	@Autowired
	private YgProductRecommendMapper ygProductRecommendMapper;
	
	@Override
	public List<YgProductRecommend> find() {
		YgProductRecommendExample example = new YgProductRecommendExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductRecommendMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgProductRecommendExample example = new YgProductRecommendExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductRecommendMapper.countByExample(example);
	}

	@Override
	public YgProductRecommend findById(Long id) {
		return ygProductRecommendMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(YgProductRecommend ygProductRecommend) {
		return ygProductRecommendMapper.insertSelective(ygProductRecommend);
	}

	@Override
	public int update(YgProductRecommend ygProductRecommend) {
		return ygProductRecommendMapper.updateByPrimaryKeySelective(ygProductRecommend);
	}

	@Override
	public ResultSet<YgProductRecommend> search(QueryInfo queryInfo,String q) {
		List<YgProductRecommend>  list =  ygProductRecommendMapper.selectSuper(queryInfo, q);
		int count = ygProductRecommendMapper.countSuper(q);
		ResultSet<YgProductRecommend> resultSet = new ResultSet<YgProductRecommend>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgProductRecommendExample example = new YgProductRecommendExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgProductRecommend record = new YgProductRecommend();
		record.setStatus(DataStatus.Delete);

		return ygProductRecommendMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgProductRecommend ygProductRecommend) {
		int r = 0;
		if(ygProductRecommend.getId() != null) {
			r = this.update(ygProductRecommend);
		}else {
			r = this.save(ygProductRecommend);
		}
		return r;
	}

	@Override
	public YgProductRecommend findByGroupId(Long groupId) {
		return ygProductRecommendMapper.selectByGroupKey(groupId);
	}
	//@Cacheable(cacheNames = "redis", key = "'product:recommend_'+#queryInfo.start+'_'+#queryInfo.end+'_'+#groupId+'_'+#productId")
	@Override
	public List<YgProductRecommendModel> findProductByGroupId(QueryInfo queryInfo, Long groupId) {
		return ygProductRecommendMapper.selectProductByGroupKey(queryInfo, groupId);
	}

}

